package com.soft.collateral.service.impl;

import com.soft.collateral.exception.CollateralException;
import com.soft.collateral.service.FileStrategyService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/8/3
 */
@Service
public class FileUploadContext {

    //键 -> 策略在IOC容器中的beanName   值->策略
    private final Map<String, FileStrategyService> strategies;

    public FileUploadContext(Map<String, FileStrategyService> strategies) {
        this.strategies = strategies;
    }

    //上传
    public String upload(MultipartFile file , String provider){
        FileStrategyService uploadStrategy = strategies.get(provider);
        if(uploadStrategy == null){
            throw new CollateralException("未知云服务运营商："+provider);
        }
        return uploadStrategy.uploadFile(file);
    }

    //删除
    public boolean delete(String url , String provider){
        FileStrategyService uploadStrategy = strategies.get(provider);
        if(uploadStrategy == null){
            throw new CollateralException("未知云服务运营商："+provider);
        }
        return uploadStrategy.deleteFile(url);
    }
}